from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
from django.core.urlresolvers import reverse
from django.utils.html import strip_tags
from pyplog.photos.models import Artist, Gallery, License, Location, Photo, Tag
from pyplog.photos.views import key_classes
from pyplog.config import config

class Photos(Feed):
	title = config.title
	description =  "All photos"
	
	def items(self):
		return Photo.public_objects.all()[:config.feed_items]

	def link(self):
		return reverse("gallery-index")
	
class PhotosByGenericKey(Feed):
	title = config.title
	
	def get_object(self, bits):
		if len(bits) == 1:
			try:
				return self.key_class.objects.get(slug = bits[0])
			except self.key_class.DoesNotExist:
				pass
		raise FeedDoesNotExist
	
	def description(self, obj = None):
		return strip_tags(obj.photoset_description)
	
	def items(self, obj = None):
		return obj.photos.all()[:config.feed_items]
	
	def link(self, obj):
		return obj.get_absolute_url()

class PhotosByArtist(PhotosByGenericKey):
	key_class = Artist

class PhotosByGallery(PhotosByGenericKey):
	key_class = Gallery
	
class PhotosByLicense(PhotosByGenericKey):
	key_class = License
	
class PhotosByLocation(PhotosByGenericKey):
	key_class = Location
	
class PhotosByTag(PhotosByGenericKey):
	key_class = Tag
